// pages/mine/record/seat/seat.js
const app = getApp()
const http = require('../../../../utils/util')
const Base64 = require('../../../../utils/base')
Page({

    /**
     * 页面的初始数据
     */
    data: {
        type: 0, //选项卡选中项
        page: 1,
        orderList: [],
		status:0,
		isnext:true,
		paymentClass:'none',
		pay_type:1, //支付方式
		current_order:'', //选中支付的订单
		seatOrRomm:['全部类型','座位订单','会议室订单'],
		pickerIndex:0,
		orderStatus:['全部','待支付','待使用','已完成'],
		pickerStatusIndex:0,
		ordertype:0, //0默认 1座位订单 2会议室订单
    },
	//获取数据
    getList: function () {
        let data = {
            user_id: wx.getStorageSync('user_id'),
            user_token: wx.getStorageSync('user_token'),
            status: this.data.status,
            type: this.data.ordertype, 
            page: this.data.page,
        }
        http.post('/user/order',Base64.encode(JSON.stringify(data))).then(res => {
			//判断是否有下一页
			if(res.result.length==0){ 
				this.setData({
					isnext:false
				})
				return
			}
            if(res.status==1){
            	this.setData({
            		orderList:[...this.data.orderList,...res.result]
            	})
            }else{
            	wx.showToast({
            		title:'获取数据失败'
            	})
            }
        })
    },
	//筛选座位订单，会议室订单
	changeType(e){
		this.setData({
			ordertype:e.detail.value,
			pickerIndex:e.detail.value,
			page:1,
			isnext:true,
			orderList:[]
		},()=>{
			this.getList()
		})
	},
	//筛选订单状态
	changeStatus(e){
		this.setData({
			status:e.detail.value,
			pickerStatusIndex:e.detail.value,
			page:1,
			isnext:true,
			orderList:[]
		},()=>{
			this.getList()
		})
	},
	//选择支付方式
	changePayMethods(e){
		this.setData({
			pay_type:e.detail.value
		})
	},
	//显示支付方式
	showPayMethods(e){
		this.setData({
			paymentClass:'show',
			current_order:e.currentTarget.dataset.order
		})
	},
	// 关闭支付方式
	hidePayMethods(){
		this.setData({
			paymentClass:'hide'
		})
		setTimeout(()=>{
			this.setData({
				paymentClass:'none'
			})
		},200)
	},
	//支付
	payments(){
		let _this=this
		
		//判断座位订单or会议室订单 准备参数
		let data=null
		let url=null
		if(this.data.current_order.genre_id==0){ //会议室
		    url='user/updateRoom'
			data={
				user_id:wx.getStorageSync('user_id'),
				user_token:wx.getStorageSync('user_token'),
				room_id:this.data.current_order.id,
				type:this.data.pay_type
			}
		}else{ //座位订单
		    url='user/updateOrder'
			data={
				user_id:wx.getStorageSync('user_id'),
				user_token:wx.getStorageSync('user_token'),
				order_id:this.data.current_order.id,
				type:this.data.pay_type
			}
		}
		
		//支付逻辑
		http.post(url,Base64.encode(JSON.stringify(data))).then((res)=>{
			console.log(res);
			if(res.status==1){
				if(this.data.pay_type==1){ //微信支付
					let data=res.result
					wx.requestPayment({
					  timeStamp: data.timeStamp,
					  nonceStr: data.nonceStr,
					  package: data.package,
					  signType: data.signType,
					  paySign: data.paySign,
					  success:(res)=>{
						  _this.setData({
						  	paymentClass:'none',
						  	page:1,
						  	isnext:true,
							orderList: []
						  },()=>{
							  _this.getList()
						  })
						  
					  },
					  fail:()=>{
						  wx.showToast({
							  title:'支付失败',
							  icon:'none'
						  })
					  }
					})
				}else{ //储蓄卡支付
					wx.showToast({
						title:'支付成功',
						icon:'none'
					})
					
					// 刷新订单 关闭弹出层
					_this.setData({
						paymentClass:'none',
						page:1,
						isnext:true,
						orderList: []
					},()=>{
						_this.getList()
					})
					
				}
			}else{
				wx.showToast({
					title:res.message,
					icon:'none'
				})
			}
		})
	},
	
    /**
     * 生命周期函数--监听页面加载
     */
    onLoad: function (options) {
		
    },
	onShow(){
		this.setData({
			page:1,
			isnext:true,
			orderList:[]
		},()=>{
			this.getList()
		})
	},
	//切换订单状态tabbar
  //   tabBar: function (e) {
		// let status=e.currentTarget.dataset.type
  //       this.setData({
		// 	type:e.currentTarget.dataset.type,
  //           page: 1,
  //           status: status,
		// 	isnext:true,
		// 	orderList:[]
  //       })
  //       this.getList()
  //   },
    orderDetail:function (e) {
		let item=e.currentTarget.dataset.item
		
		let params={
			order_id:item.id,
			type:item.type
		}
        wx.navigateTo({ 
            url: '/pages/mine/record/recordorderdetails/recordorderdetails?params='+JSON.stringify(params)
          })
    },
	
	//页面上拉加载
	onReachBottom(){
		if(this.data.isnext){
			this.setData({
				page:++this.data.page
			})
			this.getList()
		}
	},
	
	onUnload(){
		
	},
    /**
     * 用户点击右上角分享
     */
    onShareAppMessage: function () {

    }
})